/**
 * useAuth hook tests
 */

import React from 'react';
import { renderHook, act } from '@testing-library/react';
import '@testing-library/jest-dom';

// Mock localStorage
const localStorageMock = {
  getItem: jest.fn(),
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
};
Object.defineProperty(window, 'localStorage', { value: localStorageMock });

// Simple useAuth hook implementation for testing
interface User {
  user_id: string;
  email: string;
  name: string;
}

const useAuth = () => {
  const [user, setUser] = React.useState<User | null>(null);
  const [token, setToken] = React.useState<string | null>(null);
  const [isAuthenticated, setIsAuthenticated] = React.useState(false);

  const login = async (credentials: { username: string; password: string }) => {
    // Mock login logic
    const mockUser: User = {
      user_id: 'test-user-123',
      email: 'test@example.com',
      name: 'Test User'
    };
    const mockToken = 'mock-jwt-token';

    setUser(mockUser);
    setToken(mockToken);
    setIsAuthenticated(true);

    // Store in localStorage
    localStorage.setItem('user', JSON.stringify(mockUser));
    localStorage.setItem('token', mockToken);
  };

  const logout = () => {
    setUser(null);
    setToken(null);
    setIsAuthenticated(false);

    // Clear localStorage
    localStorage.removeItem('user');
    localStorage.removeItem('token');
  };

  return {
    user,
    token,
    isAuthenticated,
    login,
    logout
  };
};

describe('useAuth Hook', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  test('initializes with null values', () => {
    const { result } = renderHook(() => useAuth());

    expect(result.current.user).toBeNull();
    expect(result.current.token).toBeNull();
    expect(result.current.isAuthenticated).toBe(false);
  });

  test('handles login successfully', async () => {
    const { result } = renderHook(() => useAuth());

    await act(async () => {
      await result.current.login({ username: 'test', password: 'test' });
    });

    expect(result.current.user).toEqual({
      user_id: 'test-user-123',
      email: 'test@example.com',
      name: 'Test User'
    });
    expect(result.current.token).toBe('mock-jwt-token');
    expect(result.current.isAuthenticated).toBe(true);
    expect(localStorageMock.setItem).toHaveBeenCalledWith('user', JSON.stringify({
      user_id: 'test-user-123',
      email: 'test@example.com',
      name: 'Test User'
    }));
    expect(localStorageMock.setItem).toHaveBeenCalledWith('token', 'mock-jwt-token');
  });

  test('handles logout successfully', async () => {
    const { result } = renderHook(() => useAuth());

    // First login
    await act(async () => {
      await result.current.login({ username: 'test', password: 'test' });
    });

    // Then logout
    act(() => {
      result.current.logout();
    });

    expect(result.current.user).toBeNull();
    expect(result.current.token).toBeNull();
    expect(result.current.isAuthenticated).toBe(false);
    expect(localStorageMock.removeItem).toHaveBeenCalledWith('user');
    expect(localStorageMock.removeItem).toHaveBeenCalledWith('token');
  });
});